package com.hxzy.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.yygh.common.result.Result;
import com.hxzy.yygh.common.util.MD5;
import com.hxzy.yygh.hosp.service.HospitalSetService;
import com.hxzy.yygh.model.hosp.HospitalSet;
import com.hxzy.yygh.vo.hosp.HospitalSetQueryVo;
import com.hxzy.yygh.vo.order.SignInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置模块")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //查询全部医院设置接口
    @GetMapping("findAll")
    @ApiOperation(value = "查询全部医院设置接口")
    public Result findAll(){
        List<HospitalSet> hospitalSetList = hospitalSetService.list();

        return Result.ok(hospitalSetList);
    }

    //删除医院设置接口
    @ApiOperation(value = "删除医院设置接口")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable Long id){
        hospitalSetService.removeById(id);
        return Result.ok();
    }

    //条件分页查询接口
    @ApiOperation(value = "条件分页查询医院设置接口")
    @GetMapping("selectByPage/{page}/{limit}")
    public Result selectByPage(@PathVariable Long page, @PathVariable Long limit, HospitalSetQueryVo hospitalSetQueryVo){
        //分页构造器
        Page<HospitalSet> hospitalSetPage = new Page<>(page,limit);

        //条件构造器
        LambdaQueryWrapper<HospitalSet> lqw = new LambdaQueryWrapper<>();
        //医院名称模糊查询条件
        lqw.like(hospitalSetQueryVo.getHosname() != null, HospitalSet::getHosname, hospitalSetQueryVo.getHosname());
        //医院编号匹配查询条件
        lqw.eq(hospitalSetQueryVo.getHoscode() != null, HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());
        //根据更新时间排序
        lqw.orderByDesc(HospitalSet::getUpdateTime);

        //执行分页条件
        Page<HospitalSet> setPage = hospitalSetService.page(hospitalSetPage, lqw);

        //返回分页结果
        return Result.ok(setPage);
    }

    //添加医院设置接口
    @ApiOperation(value = "添加医院设置接口")
    @PostMapping("save")
    public Result save(@RequestBody HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);

        //签名密钥加密
        Random random = new Random();
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));

        //设置签名密钥
        hospitalSet.setSignKey(encrypt);

        //调用新增方法
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //根据id获取医院设置接口
    @ApiOperation(value = "根据id查询医院设置接口")
    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //修改医院设置接口
    @ApiOperation(value = "修改医院设置接口")
    @PutMapping("update")
    public Result update(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //批量删除医院设置接口
    @ApiOperation(value = "批量删除医院设置接口")
    @DeleteMapping("batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids){
        boolean b = hospitalSetService.removeByIds(ids);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁接口")
    @PutMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok().message("修改医院状态成功!");
    }

    //发送签名密钥
    @ApiOperation(value = "发送签名密钥接口")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //TODO 发送短信
        return Result.ok();
    }

    //获取医院签名信息
    @ApiOperation(value = "获取医院签名信息")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable("hoscode") String hoscode) {
        return hospitalSetService.getSignInfoVo(hoscode);
    }


}

